<template>
  <ContainerGroup>
    <Container style="margin-bottom: 10px">
      <FilterForm
        ref="filterForm"
        :elements="filterElements"
        @submit="handleSearch"
        @reset="handlerReset"
      >
        <Button
          type="info"
          icon="add"
          @click="insert"
        >新增</Button>

      </FilterForm>
    </Container>
    <Container height="fill" display="flexY">
      <Table
        :index="true"
        :columns="tableColumns"
        :loading="tableLoading"
        :data="tableData"
        :pagination="{
          total: total,
          current: currentPage,
          pageSize: pageSize
        }"
        @paginationChange="pageChange"
      >
        <template #initialBalance="{ row }">{{
          row.initialBalance | numberToCurrency
        }}</template>
        <template #endBalance="{ row }">{{
          row.endBalance | numberToCurrency
        }}</template>
        <template #deptCode="{ row }">
          {{ row.deptCode }}
        </template>
        <template #action="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <i class="el-icon-edit action" @click="edit(scope.row)" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <i class="el-icon-delete action" @click="remove(scope.row)" />
          </el-tooltip>
        </template>
      </Table>
      <Drawer
        :visible="drawerVisible"
        :title="drawerTitle"
        :footer-btns="drawerController"
        width="30%"
        @cancel="drawerCancel"
        @submit="drawerSubmit"
      >
        <div class="draw-body">
          <el-form
            ref="formData"
            label-width="80px"
            :model="formData"
            :rules="rules"
            @keyup.enter.native="drawerSubmit('formData')"
          >
            <el-form-item label="科室编号" prop="deptCode">
              <el-input
                ref="deptCode"
                v-model="formData.deptCode"
                style="width: 240px"
                size="small"
              />
            </el-form-item>

            <el-form-item label="科室名称" prop="deptName">
              <el-input
                v-model="formData.deptName"
                style="width: 240px"
                size="small"
              />
            </el-form-item>

            <el-form-item label="期初金额" prop="initialBalance">
              <el-input
                v-model="formData.initialBalance"
                style="width: 240px"
                size="small"
              />
            </el-form-item>

          </el-form>
        </div>
      </Drawer>
    </Container>
  </ContainerGroup>
</template>

<script>
import { listAllDept, listPrimusCash, saveOrUpdate, remove } from '@/apis/kzrFound'
export default {
  name: 'ProjectDataSet', // 科主任基金2019期初设置

  data() {
    return {
      filterElements: [
        {
          key: 'deptCode',
          type: 'Select',
          label: '科室',
          value: '',
          itemLabel: 'deptName',
          itemValue: 'deptCode',
          options: [],
          filterable: true,
          clearable: true,
          width: 250
        }
      ],
      tableColumns: [
        {
          title: '科室编码',
          dataIndex: 'deptCode',
          type: 'otherTag'
        },
        {
          title: '科室名称',
          dataIndex: 'deptName'
        },
        {
          title: '2019期初金额',
          dataIndex: 'initialBalance',
          type: 'otherTag'
        },
        {
          dataIndex: 'action',
          title: '操作',
          align: 'center',
          type: 'otherTag',
          width: 180
        }
      ],
      formData: {
        deptCode: '',
        deptName: '',
        initialBalance: 0
      },
      rules: {
        deptCode: [
          { required: true, message: '请输入科室编号', trigger: 'change' }
        ],
        deptName: [
          { required: true, message: '请输入科室名称', trigger: 'change' }
        ],
        initialBalance: [
          { required: true, message: '请输入期初金额', trigger: 'change' }
        ]
      },
      drawerController: [
        {
          text: '提交',
          trigger: 'submit',
          type: 'primary'
        }
      ],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      tableData: [],
      dcLoading: false,
      tableLoading: false,
      newCondition: {},

      currentId: '',
      isEdit: false,
      drawerVisible: false,
      drawerTitle: ''
    }
  },
  mounted() {
    this.getListAllDept()
  },
  methods: {
    getListAllDept() {
      listAllDept()
        .then(res => {
          if (res.data.code === 200) {
            this.filterElements[0].options = res.data.data
          } else {
            this.$message.error('科室获取失败')
          }
        })
        .catch(() => {
          this.$message.error('科室获取失败')
        })
    },
    getData() {
      const params = {
        deptCode: this.newCondition?.deptCode,
        pageIndex: this.currentPage,
        pageSize: this.pageSize
      }
      this.tableLoading = true
      listPrimusCash(params).then(res => {
        this.tableLoading = false
        if (res.data.code === 200) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.$message.error('数据获取失败')
        }
      }).catch(() => {
        this.tableLoading = false
        this.$message.error('数据获取失败')
      })
    },
    handleSearch(value) {
      this.newCondition = value
      this.currentPage = 1
      this.getData()
    },
    handlerReset(val) {},
    handleExport() {},
    pageChange(payload) {
      this.currentPage = payload.current
      this.pageSize = payload.pageSize
      this.getData()
    },
    insert() {
      this.$refs.formData.resetFields()
      this.isEdit = false
      this.drawerTitle = '新增'
      this.drawerVisible = true
      this.setFocus()
    },
    edit(row) {
      this.isEdit = true
      this.currentId = row.id
      this.drawerTitle = '编辑'
      this.drawerVisible = true
      this.formData = JSON.parse(JSON.stringify(row))
      this.setFocus()
    },
    remove(row) {
      this.$confirm({
        content: '请输入“立即删除”，确认删除',
        title: '提示',
        action: 'delete',

        okFn: async(res) => {
          const params = {
            id: row.id
          }

          remove(params).then((res) => {
            if (res.data.code === 200) {
              this.$message.success('删除成功')
              this.getData()
            } else {
              this.$message.warning(res.data.msg)
            }
          })
        }
      })
    },
    // 设置聚焦
    setFocus() {
      this.$nextTick(() => {
        this.$refs.deptCode.$el.children[0].focus()
      })
    },
    drawerCancel() {
      this.drawerVisible = false
      this.imgList = []
      this.getData()
    },
    drawerSubmit() {
      const params = {
        deptCode: this.formData.deptCode,
        deptName: this.formData.deptName,
        initialBalance: this.formData.initialBalance
      }
      this.$refs.formData.validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            params.id = this.currentId
            saveOrUpdate(params).then((res) => {
              if (res.data.code === 200) {
                this.$message.success('修改成功')
                this.getData()
                this.drawerVisible = false
              } else {
                this.$message.warning(res.data.msg)
                this.drawerVisible = false
              }
            })
          } else {
            saveOrUpdate(params).then((res) => {
              if (res.data.code === 200) {
                this.$message.success('新增成功')
                this.getData()
                this.$refs.formData.resetFields()
                this.setFocus()
              } else {
                this.$confirm({
                  content: `${res.data.msg}`,
                  title: '异常提醒'
                })
                this.drawerVisible = false
              }
            })
          }
        } else {
          return false
        }
      })
    }

  }
}
</script>

<style lang="less" scoped>
.action {
  font-size: 16px;
  margin-right: 8px;
  cursor: pointer;
  color: #2165f9;
}
.draw-body {
  padding: 12px;
}
</style>
